package com.song.shop.controller;

import com.google.common.collect.Lists;
import com.song.shop.entity.Shop;
import com.song.shop.service.ShopService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.*;

@RestController(value = "/shop")
@PropertySource("classpath:redis.properties")
public class ShopController {

    @Autowired
    private Environment env;

    private final static Logger log = LoggerFactory.getLogger(ShopController.class);

    @Autowired
    private ShopService shopService;
	/*@Resource
	private RedisTemplate<Serializable, Object> redisTemplate;*/

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public Map<String, Object> testData() {
        Map<String, Object> map = new HashMap<>();
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        String property = env.getProperty("test");
        System.out.println(env.getProperty("spring.redis.host"));
        operations.set("property", property + "你好");
        map.put("data", property);
        return map;


    }

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    public Shop getOne(HttpServletRequest request) {
        log.info("init");
        //Shop shop = shopService.getOne();
        request.getSession().setAttribute("songName" , "测试数据name");
        HttpSession session = request.getSession();
        System.out.println(session);
        return new Shop();
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public List<Shop> getAll(HttpServletRequest request) {
        List<Shop> list = shopService.getAll();
        return list;
    }

    @RequestMapping(value = "/insertShop", method = RequestMethod.GET)
    public String insertShop(Shop shop) throws Exception {
        shopService.getOne();
        shop.setId(UUID.randomUUID().toString());
        shopService.insertShop(shop);
        return "success111";
    }

    @RequestMapping(value = "/insertShop2", method = RequestMethod.GET)
    public String insertShop2(Shop shop) {
        shop.setId(UUID.randomUUID().toString());
        shopService.insertShop2(shop);
        return "success222";
    }


    @RequestMapping(value = "/updateShop", method = RequestMethod.POST)
    public String updateShop(Shop shop) {
        try {
            shopService.updateShop(shop);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    @RequestMapping(value = "/test3", method = RequestMethod.GET)
    public String test3() throws Exception {
        Shop shop = new Shop();
        shop.setId("00000009");
        shop.setShopName("测试回滚异常");
        shopService.insertShop(shop);
        System.out.println("测试异常后的代码执行情况");
        return "success";
    }

    @RequestMapping(value = "/test4")
    public List<Shop> tset4(){
        ArrayList<String> ids = Lists.newArrayList();
        return shopService.queryByIds(ids);

    }

    @RequestMapping(value = "/getCookies")
    public String getCookies(HttpServletRequest request){
        request.getSession().setAttribute("name","测试数据name");
        return "success";
    }

    @RequestMapping(value = "getData")
    public String getData(HttpServletRequest request){
        Object name = request.getSession().getAttribute("name");
        System.out.println(Boolean.TRUE.toString());
        return "sucess"+name+Boolean.TRUE.toString();
    }


}
